package com.bkd.dreams.client.stand;

import java.util.List;

import com.bkd.dreams.client.ComponentRenderer;
import com.bkd.dreams.client.stand.service.StandService;
import com.bkd.dreams.client.stand.service.StandServiceAsync;
import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class StandTable extends ComponentRenderer {

	private static final String TAB_NAME = "Stands";
	private static final Class<StandService> SERVICE_CLASS = StandService.class;
	private VerticalPanel mainPanel = new VerticalPanel();
	private CellTable<Stand> table = new CellTable<Stand>();
	private HorizontalPanel editPanel = new HorizontalPanel();
	private TextBox newStandNameTextBox = new TextBox();
	private TextArea newStandDescriptionTextArea = new TextArea();
	
	private Button addButton = new Button("Add");

	private StandServiceAsync service = GWT.create(StandService.class);

	@Override
	public void loadTable(TabPanel tabPanel) {
		TextColumn<Stand> nameColumn = new TextColumn<Stand>() {
			@Override
			public String getValue(Stand stand) {
				return stand.getName();
			}
		};
		table.addColumn(nameColumn, "Stand");
		
		TextColumn<Stand> descColumn = new TextColumn<Stand>() {
			@Override
			public String getValue(Stand stand) {
				return stand.getDescription();
			}
		};
		table.addColumn(descColumn, "Description");

		ActionCell<Stand> actionCell = new ActionCell<Stand>(
				"Remove", new ActionCell.Delegate<Stand>() {
					public void execute(Stand stand) {
						removeStand(stand);
					}

				});
		Column<Stand, Stand> removeColumn = new Column<Stand, Stand>(actionCell) {
			public Stand getValue(Stand stand) {
				return stand;
			}
		};
		table.addColumn(removeColumn, "Remove");

		editPanel.add(newStandNameTextBox);
		editPanel.add(newStandDescriptionTextArea);
		editPanel.add(addButton);

		mainPanel.add(table);
		mainPanel.add(editPanel);
		
		tabPanel.add(mainPanel, TAB_NAME);
		
		addButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addStand();
			}
		});
		
		refreshStandTable();
	}

	private void refreshStandTable() {
		if (service == null) {
			service = GWT.create(StandService.class);
		}

		// Set up the callback object.
		AsyncCallback<List<Stand>> callback = new AsyncCallback<List<Stand>>() {
			public void onFailure(Throwable error) {
				handleError(error);
			}

			public void onSuccess(List<Stand> result) {
				updateStandTable(result);
			}
		};

		// Make the call to the stock price service.
		service.getAllStands(callback);		
	}

	protected void updateStandTable(List<Stand> stands) {
		if (stands != null) {
			table.setRowCount(stands.size(),true);
			table.setRowData(0, stands);
		} else {
			table.setRowCount(0);
		}
	}

	protected void addStand() {
		String name = newStandNameTextBox.getText();
		String desc = newStandDescriptionTextArea.getText();
		if(name.equals("")) {
			return;
		}
		
		Stand stand = new Stand(name, desc);
		addStand(stand);
		newStandNameTextBox.setText("");
		newStandDescriptionTextArea.setText("");
	}

	private void addStand(Stand stand) {
		if (service == null) {
			service = GWT.create(StandService.class);
		}

		// Set up the callback object.
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
			public void onFailure(Throwable error) {
				handleError(error);
			}

			public void onSuccess(Void result) {
				refreshStandTable();
			}
		};

		// Make the call to the stock price service.
		service.addStand(stand, callback);
	}

	private void removeStand(Stand stand) {
		if (service == null) {
			service = GWT.create(StandService.class);
		}

		// Set up the callback object.
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
			public void onFailure(Throwable error) {
				handleError(error);
			}

			public void onSuccess(Void result) {
				refreshStandTable();
			}
		};

		// Make the call to the stock price service.
		service.removeStand(stand, callback);
	}
}
